package com.liang.config;



import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/**
 * mvc拦截器，只拦截controller请求，不会拦截静态资源
 * 动态代理原理，通过aop注入mvc并使用
 */
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String name = (String) request.getSession().getAttribute("name");
        if(name!=null){
            return true;
        }
        if(request.getRequestURI().contains("login")){
            return true;
        }
        request.getRequestDispatcher("/").forward(request,response);
        return false;
    }

}
